package it.aekidna.cleverplatypus.gui.containers.commonpopup
{
	import flash.utils.Dictionary;
	
	import it.aekidna.cleverplatypus.gui.containers.ContextualPopUpPanel;
	
	import as3reflect.ClassUtils;
	
	public class PopupDialogFactory
	{
		private static var _dialogues:Dictionary = new Dictionary(false);
		
		public static function gimme(contract:Class):ContextualPopUpPanel
		{
			if(_dialogues[contract] && (_dialogues[contract] as Array).length)
				return (_dialogues[contract] as Array).pop() as ContextualPopUpPanel;
			else {
				if(contract)
					return new contract();
			}
			
			return null;
		}
		
		public static function recycle(instance:ContextualPopUpPanel):void
		{
			if(instance) {
				var classOfInstance:Class = ClassUtils.forInstance( instance );
				if(!_dialogues[classOfInstance])
					_dialogues[classOfInstance] = [];
				
				//TODO: check if the instance is not already there!
				var instances:Array = _dialogues[classOfInstance] as Array;
				if(instances)
					instances.push(instance);
			}
		}

	}
}